const path = require('path');
const filterFileList = require('./tools');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerWebpackPlugin = require("css-minimizer-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const internalIp = require('internal-ip');
const tempList = [];
const entry = {};
const htmlPlugins = [];
filterFileList('./src', tempList);

const targetList = tempList.filter(path => {
    if (path.includes('docsSharing') || path.includes('whiteboard')) {
        return false;
    } else {
        return true;
    }
});


entry['content'] = targetList.find(item => item.endsWith('content.js'));
targetList
    .filter(item => item.endsWith('index.js'))
    .forEach(p => {
        const regResult = /.+src[\/|\\](.+)[\/|\\]index\.js$/.exec(p);
        if (regResult && regResult[1]) {
            regResult[0] = regResult[0].replace(/\/index\.js$/, "")
            entry[regResult[1]] = regResult[0];
        }
    });

targetList
    .filter(item => item.endsWith('index.html'))
    .forEach(tepmlate => {
        const regResult = /.+src[\/|\\](.+)[\/|\\]index.html$/.exec(tepmlate);
        if (regResult && regResult[1]) {
            htmlPlugins.push(
                new HtmlWebpackPlugin({
                    template: tepmlate,
                    chunks: [regResult[1]],
                    filename: regResult[1] + '/index.html'
                })
            );
        } else if (tepmlate.includes('src/index.html')) {
            htmlPlugins.push(
                new HtmlWebpackPlugin({
                    template: './src/index.html',
                    filename: 'index.html',
                    chunks: ['content'],
                    favicon: './src/favicon.ico'
                })
            );
        }
    });

module.exports = {
    entry,
    // entry: {
    //     rangeAudio: "/Users/zego/Documents/WorkspaceWeb/alpha-express-webrtc-sample/src/rangeAudio"
    // },
    mode: 'development',
    output: {
        filename: '[name]/[name].bundle.js',
        path: path.resolve(__dirname, 'docs'),
        libraryTarget: "umd",
        umdNamedDefine: true,
        globalObject: "typeof self !== 'undefined' ? self : this"
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.js']
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }
            },
            {
                test: /\.css$/,
                use: [
                    // MiniCssExtractPlugin.loader,
                    { loader: "style-loader" }, // 如果想要在运行时动态插入 style 标签，就使用这个加载器
                    {
                        loader: 'css-loader'
                        // options: {
                        //     url: false,
                        // },
                    }
                ]
            },
            {
                test: /\.(gif|png|jpe?g|svg)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8192,
                            name: '[name].[ext]',
                            fallback: 'file-loader', //超过了限制大小调用回调函数
                            outputPath: 'public/images' //片存储的地址
                        }
                    }
                ]
            },
            {
                test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 10000, // 小于10000 ／ 1024 kb的字体会被url-loader压缩成base64格式
                            name: 'static/font/[name].[hash:7].[ext]' //字体名字，7位哈希值，扩展名
                        }
                    }
                ]
            },
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            },
            {
                test: require.resolve('jquery'),
                loader: 'expose-loader?$!expose-loader?jQuery'
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(),
        ...htmlPlugins,
        // new MiniCssExtractPlugin({
        //     filename: path.resolve(__dirname, "[name].css"), // 指定生成的 CSS 文件名
        // }),
        new CopyWebpackPlugin({
            patterns: [
                {
                    from:
                        "./node_modules/zego-express-engine-webrtc/background-process/assets"
                    ,
                    to: "./assets"
                },
                {
                    from:
                        "./src/assets/zego-express-player"
                    ,
                    to: "./assets/zego-express-player"
                }
                // { from: './src/docsSharing', to: './docsSharing' },
                // { from: './src/whiteboard', to: './whiteboard' },
            ]
        }),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.$': 'jquery',
            'window.jQuery': 'jquery'

        })
        // new HtmlWebpackPlugin({

        //     template: "/Users/zego/Documents/WorkspaceWeb/alpha-express-webrtc-sample/src/rangeAudio/index.html",
        //     chunks: ["rangeAudio"],
        //     filename: 'rangeAudio/index.html',
        // }),
    ],
    // devtool: 'eval-source-map',
    optimization: {
        minimizer: [
            // new CssMinimizerWebpackPlugin() // 压缩生成的 CSS 文件
        ]
    },
    devServer: {
        disableHostCheck: true,
        contentBase: './docs',
        port: 9092,
        host: internalIp.v4.sync(),
        https: true,
        open: true
    }
};
